package com.mini.grpc.spring.autoconfigure;

import com.mini.grpc.spring.client.GrpcClientRegistry;
import com.mini.grpc.spring.server.GrpcServerManager;
import com.mini.grpc.spring.server.GrpcServiceRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Mini-gRPC自动配置类
 * 
 * @author Mini-gRPC
 */
@Configuration
@ConditionalOnClass(name = "com.mini.grpc.server.Server")
@EnableConfigurationProperties({GrpcServerProperties.class, GrpcClientProperties.class})
public class MiniGrpcAutoConfiguration {
    
    /**
     * 服务端自动配置
     */
    @Configuration
    @ConditionalOnProperty(prefix = "mini.grpc.server", name = "enabled", havingValue = "true", matchIfMissing = true)
    public static class ServerAutoConfiguration {
        
        @Bean
        @ConditionalOnMissingBean
        public GrpcServiceRegistry grpcServiceRegistry() {
            return new GrpcServiceRegistry();
        }
        
        @Bean
        @ConditionalOnMissingBean
        public GrpcServerManager grpcServerManager() {
            return new GrpcServerManager();
        }
    }
    
    /**
     * 客户端自动配置
     */
    @Configuration
    @ConditionalOnProperty(prefix = "mini.grpc.client", name = "enabled", havingValue = "true", matchIfMissing = true)
    public static class ClientAutoConfiguration {
        
        @Bean
        @ConditionalOnMissingBean
        public GrpcClientRegistry grpcClientRegistry() {
            return new GrpcClientRegistry();
        }
    }
} 